--[[
FilePath: \Lua\View\PasteFaceNotice\UILoginNoticeThree.lua
糊脸第三个弹框
--]]

require 'View/UIBase/UIBase'

UILoginNoticeThree = UIBase.New(PanelResNames.UILoginNoticeThree)
local _M = UILoginNoticeThree

_M.hasInit = false
_M.data = nil;

function _M.Init(data)
    _M.data = data;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgClose"), self.OnClose);
        self.lihui = self:GetChild("lihui");
        self.lihui2 = self:GetChild("lihui2");
        self.bg = self:GetChild("BG");
        self.titleTop = self:GetChild("titleTop");
        self.tips = self:GetChild("tips");
        self.tipTime = self:GetChild("tipTime");
        self.btnChallenge = self:GetChild("btn_challenge");
        _M.hasInit = true;

        self:addEvent()
    end
end

function _M:addEvent()
    self.btnChallenge.onClick:Add(self.onChallenge, self);
end

function _M:removeEvent()
    self.btnChallenge.onClick:Remove(self.onChallenge);
end

function _M:onChallenge()
    logicMgr.PasteFaceNoticeManager.resetPasteFaceNotice();
    logicMgr.ActiveManager.resetActivePopUpTips();
    logicMgr.CostManager.resetActivePopUpTips();

    if self.loginNotice.f_NoticeID and self.loginNotice.f_NoticeID ~= "" then
        showUI(self.loginNotice.f_NoticeFGUI, nil, self.loginNotice.f_NoticeID);
    else
        showUI(self.loginNotice.f_NoticeFGUI);
    end
    self:loginNoticeTA(1);
    self:OnBack();
end

-- 显示
function _M:OnEnable()
    if self.data == nil or #self.data < 1 then
        self:OnClose();
        return;
    end
    self:refresh();
end

function _M:refresh()
    table.sort(self.data, function(a, b)
        return a.sort < b.sort;
    end);
    local data = self.data[1];
    self.loginNotice = G.dataTable["t_loginnotice"][tostring(data.id)];
    if self.loginNotice == nil then
        self:OnClose();
        return;
    end
    logicMgr.PasteFaceNoticeManager.setPasteFaceNoticeStatus(self.loginNotice, data.endTime);
    local serverTime = HelperFunc.GetServerTime();
    if serverTime < data.time or tonumber(self.loginNotice.f_NoticeShowBtn) == 0 then
        self.btnChallenge.visible = false;
    end

    if self.loginNotice.f_NoticeShowPic ~= nil and self.loginNotice.f_NoticeShowPic ~= "" then
        self.bg.url = self.loginNotice.f_NoticeShowPic;
    end
    self.titleTop.text = getLanguage(self.loginNotice.f_NoticeName);
    self.tips.text = getLanguage(self.loginNotice.f_NoticeDesc);
    if self.loginNotice.f_Hero ~= nil and self.loginNotice.f_Hero ~= "" then
        if self.loginNotice.f_Hero[2] and self.loginNotice.f_Hero[2] == 1 then
            local imageName = logicMgr.HeroManager.GetHeroDrawingPath(self.loginNotice.f_Hero[1])
            if self.drawWrapper == nil then
                self.drawWrapper = GoWrapper.New()
            end

            resMgr:LoadModelPrefab(imageName, function(go)
                if(self.RoleLihui~=nil)then
                    destroyImmediate(self.RoleLihui);
                    self.RoleLihui = nil
                end
                self.RoleLihui = newObject(go);
                self.RoleLihui.name = imageName;
                Util.AddAction(self.RoleLihui,self.loginNotice.f_Hero[3],true,0.5);
                self.RoleLihui:AddComponent(TCanvas);
                self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
                Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,self.loginNotice.f_RankLocation[1],self.loginNotice.f_RankLocation[2],0);
                Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,self.loginNotice.f_Ranksize[1],self.loginNotice.f_Ranksize[2],self.loginNotice.f_Ranksize[1]);
                self.lihui:SetNativeObject(self.drawWrapper);
            end)
        elseif self.loginNotice.f_Hero[2] and self.loginNotice.f_Hero[2] == 2 then
            local list = TablePool.Get()
            string_split(self.loginNotice.f_Hero[1],"/",list)
            AddUIPackageDepend(PanelResNames.UILoginNotice,"FUI/"..list[1])
            TablePool.Release(list)
            self.lihui2.url ="ui://"..self.loginNotice.f_Hero[1];
            Util.PlayAction(self.lihui2.wrapTarget, self.loginNotice.f_Hero[3], true);
        end
    end

    local time = HelperFunc.GetServerTime();
    self.endCountDown = data.endTime - time;
    self:updateActiveCountDown();
    self:TimeCall(self.updateActiveCountDown, 1000, -1);
end

function _M:updateActiveCountDown()
    if self.endCountDown == nil then
        return;
    end
    if self.endCountDown <= 0 then
        self.tipTime.text = getLanguage("Sys_ActivityEnd");
        self:RemoveTimeCall(self.updateActiveCountDown);
    else
        self.tipTime.text = string.format(getLanguage("UIRanking10"), HelperFunc.GetSurpTimeStr(self.endCountDown));
    end
    self.endCountDown = self.endCountDown - 1;
end

function _M:OnClose()
    logicMgr.PasteFaceNoticeManager.showPasteFaceNotice();
    self:loginNoticeTA(0);
    self:OnBack();
end

function _M:loginNoticeTA(type)
    local ta = {};
    ta["click_go"] = type;
    TAManager.TrackByDictionary("login_notice_select",ta);
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    if(self.RoleLihui~=nil)then
        destroyImmediate(self.RoleLihui);
        self.RoleLihui = nil
    end
    if self.drawWrapper then
        self.drawWrapper:Dispose();
        self.drawWrapper = nil;
    end
    self:removeEvent();
    self:RemoveTimeCall(self.updateActiveCountDown);
    self.data = nil;
    self.hasInit = false;
end

return _M;